package com.androidproject.activity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.json.JSONArray;
import org.json.JSONObject;

import com.androidproject.Constantes;
import com.androidproject.LineAdapterClassement;
import com.androidproject.R;
import com.androidproject.modele.Classement;
import com.androidproject.modele.Game;

import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class ClassementActivity extends Activity {
	
	private Intent intentAccueil;
	private Intent intentClassement;
	private Intent intentCalendrier;
	private Intent intentProgrammeTV;
	private Intent intentFacebook;
	private Intent intentTwitter;
	private Classement classement;
	private ProgressDialog progressD;
	private TabHost tabHost;
	private TabSpec tspec1;
	private TabSpec tspec2;
	private TabSpec tspec3;
	
	public class TaskObject extends AsyncTask<String, Void, StringBuffer> {

		private HttpResponse response;
		private JSONObject jSonObject;
		
		@Override
		protected StringBuffer doInBackground(String... url) {
			
			StringBuffer chaine = new StringBuffer("");
			
			try{
				
				AndroidHttpClient client = AndroidHttpClient.newInstance("user-agent"); 
				URI uri = new URI(url[0]);
				HttpGet request = new HttpGet(); 
				request.setURI(uri); 
				response = client.execute(request); 
				client.close();
				
				// Get the response 
				InputStream inputStream = response.getEntity().getContent(); 
				BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 
				String line = "";
				
				while ((line = rd.readLine()) != null) { 
					chaine.append(line);
				} 
				
			} catch(Exception e){ 
				chaine.append(e.getMessage());
				
			} finally { } 
			
			return chaine;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			progressD = ProgressDialog.show(ClassementActivity.this, Constantes.WAIT_TITLE,Constantes.WAIT_MESSAGE_CALENDRIER, true);
		}
		
		@Override
		protected void onPostExecute(StringBuffer result) {
			super.onPostExecute(result);
			
			List<Game> listD2M = new ArrayList<Game>();
			List<Game> listLFH = new ArrayList<Game>();
			List<Game> listOther = new ArrayList<Game>();
			
			progressD.dismiss();
			
			try{ //Traitement du JSON 
				
				jSonObject = new JSONObject(result.toString());
				JSONArray resultatgames = new JSONArray(jSonObject.getString(Constantes.CLASSEMENT_GAME));
				
				classement = new Classement();
				
				for (int i = 0; i < resultatgames.length(); i++) {
					
					Game game = new Game(resultatgames.getJSONObject(i).getString(Constantes.CLASSEMENT_COMPETE_NAME),
										resultatgames.getJSONObject(i).getString(Constantes.CLASSEMENT_DATE_GAME),
										resultatgames.getJSONObject(i).getString(Constantes.CLASSEMENT_HEURE),
										resultatgames.getJSONObject(i).getString(Constantes.CLASSEMENT_TEAM1), 
										resultatgames.getJSONObject(i).getString(Constantes.CLASSEMENT_TEAM2), 
										resultatgames.getJSONObject(i).getString(Constantes.CLASSEMENT_SCORE1), 
										resultatgames.getJSONObject(i).getString(Constantes.CLASSEMENT_SCORE2));
					
					classement.getGameList().add(game);
					
					if(game.getCompetname().equals("D2M")){
						listD2M.add(game);
						
					} else if(game.getCompetname().equals("LFH")){
						listLFH.add(game);
						
					} else {
						listOther.add(game);
					}
					
				}

				ListView listView = (ListView) findViewById(R.id.lvOnglet1);
				listView.setAdapter(new LineAdapterClassement(ClassementActivity.this, listD2M));
				
				ListView listView2 = (ListView) findViewById(R.id.lvOnglet2);
				listView2.setAdapter(new LineAdapterClassement(ClassementActivity.this, listLFH));
				
				ListView listView3 = (ListView) findViewById(R.id.lvOnglet3);
				listView3.setAdapter(new LineAdapterClassement(ClassementActivity.this, listOther));
				
			} catch(Exception e){ 
				Log.e("Erreur",e.getMessage()); 
				
			} finally { } 
			
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_classement_with_tab);
		
		tabHost = (TabHost) this.findViewById(R.id.tabhost); 
		tabHost.setup();
		
		tspec1 = tabHost.newTabSpec("Tab1"); 
		tspec1.setIndicator("D2M"); 
		tspec1.setContent(R.id.tab1); 
		tabHost.addTab(tspec1); 
		
		tspec2 = tabHost.newTabSpec("Tab2"); 
		tspec2.setIndicator("LFH"); 
		tspec2.setContent(R.id.tab2); 
		tabHost.addTab(tspec2); 
		
		tspec3 = tabHost.newTabSpec("Tab3"); 
		tspec3.setIndicator("Autres"); 
		tspec3.setContent(R.id.tab3); 
		tabHost.addTab(tspec3);
		
		TaskObject task = new TaskObject(); 
		task.execute(new String[] {Constantes.FLUX_CLASSEMENT});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu.add(0, 1, 0, "News").setIcon(R.drawable.logo_journal);
		menu.add(0, 2, 0, "Classement").setIcon(R.drawable.logo_classement);
		menu.add(0, 3, 0, "Programme TV").setIcon(R.drawable.logo_tv);
		menu.add(0, 4, 0, "Calendrier").setIcon(R.drawable.logo_calendrier);
		menu.add(0, 5, 0, "Facebook").setIcon(R.drawable.logo_facebook);
		menu.add(0, 6, 0, "Twitter").setIcon(R.drawable.logo_twitter);
	    
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case 1:
			intentAccueil = new Intent(this, MainActivity.class);
			startActivity(intentAccueil);
			return true;
			
		case 2:
			intentClassement = new Intent(this, ClassementActivity.class);
			startActivity(intentClassement);
			return true;
			
		case 3:
			intentProgrammeTV = new Intent(this, ProgrammeTvActivity.class);
			startActivity(intentProgrammeTV);
			return true;
			
		case 4:
			intentCalendrier = new Intent(this, CalendrierActivity.class);
			startActivity(intentCalendrier);
			return true;
		
		case 5:
			intentFacebook = new Intent(this, FacebookActivity.class);
			startActivity(intentFacebook);
			return true;
			
		case 6:
			intentTwitter = new Intent(this, TwitterActivity.class);
			startActivity(intentTwitter);
			return true;
			
		default:
			return true;
		}
	}

}
